package com.hao.epidemic_prevention.security.config;

import com.hao.epidemic_prevention.security.handler.MyAccessDeniedHandler;
import com.hao.epidemic_prevention.security.handler.MyAuthenticationEntryPoint;
import com.hao.epidemic_prevention.security.handler.MyLogoutSuccessHandler;
import com.hao.epidemic_prevention.security.adminlogin.AdminServiceImpl;
import com.hao.epidemic_prevention.security.passwordencoder.MyPasswordEncoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.password.PasswordEncoder;

import javax.sql.DataSource;

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private AdminServiceImpl adminServiceImpl;

    @Autowired
    DataSource dataSource;

    @Autowired
    MyAuthenticationProvider myAuthenticationProvider;

    //  设置密码加密类
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new MyPasswordEncoder();
        // new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) {
        // 配置自定义的校验器
        auth.authenticationProvider(myAuthenticationProvider);
        //auth.userDetailsService(adminServiceImpl).passwordEncoder(passwordEncoder());
    }
    /*@Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().withUser("admin").password("{noop}123").roles("ADMIN").authorities("ADMIN");
    }*/

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        // 设置URL的授权问题
        // 多个条件取交集
        http.authorizeRequests()
                // 只能匿名登录
                .antMatchers("/admin/toLogin", "/admin/login").permitAll()
                .antMatchers("/user/toIndex", "/user/report", "/user/isReportToday", "/user/**").permitAll()
                .antMatchers("/verify").permitAll()
                .antMatchers("/login/**").permitAll()
                .antMatchers("/admin/getAllUser").hasRole("ADMIN")
                .anyRequest().authenticated();// anyRequest() 所有请求   authenticated() 必须被认证

        // 配置认证
        http.formLogin()
                //.loginPage("/admin/toLogin")
                .loginProcessingUrl("/admin/login")
                .successForwardUrl("/admin/loginSuccess")
                .failureForwardUrl("/admin/loginFail")
                .and()
                .logout()
                .logoutUrl("/admin/logout")
                .logoutSuccessHandler(new MyLogoutSuccessHandler());




        // 配置
        http.exceptionHandling()
                .authenticationEntryPoint(new MyAuthenticationEntryPoint()) //设置完整登录
                .accessDeniedHandler(new MyAccessDeniedHandler()); // 配置403访问错误处理器。

        // 关掉csrf安全协议
        http.csrf().disable();
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        super.configure(web);
    }

}
